﻿using Newtonsoft.Json;
using System;

namespace Com.Lancetinc.Evs.Helpers;

public static class JsonUtil
{
    public static void Init()
    {
        var setting = new JsonSerializerSettings();
        JsonConvert.DefaultSettings = () =>
        {
            //空值处理
            setting.NullValueHandling = NullValueHandling.Ignore;
            setting.DateFormatString = "yyyy-MM-dd HH:mm:ss.fff";
            setting.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            return setting;
        };
    }

    public static T Parse<T>(string str)
    {
        return JsonConvert.DeserializeObject<T>(str);
    }

    public static object Parse(string str, Type type)
    {
        return JsonConvert.DeserializeObject(str, type);
    }

    public static string Stringify(object obj, bool indented = false)
    {
        var fmt = indented ? Formatting.Indented : Formatting.None;
        return JsonConvert.SerializeObject(obj, fmt);
    }
}